const common = require('common');
cc.Class({
    extends: cc.Component,

    properties: {
        book_prefab: cc.Prefab,
    },

    // use this for initialization
    onLoad: function() {

    },
    init: function() {
        //先清空
        cc.log('-----bookshelfcontent init----');
        let self = this;
        //jsb.fileUtils.writeStringToFile(JSON.stringify( booklocal),jsb.fileUtils.getWritablePath()+"bookdata.json");
        //var common = cc.find('common');
        // cc.game.addPersistRootNode(this.common);
        // cc.loader.load(jsb.fileUtils.getWritablePath() + "bookdata.json", function(err, booklocal) {
        //     self.common.bookdata = booklocal;
        for (var i = common.init.bookdata.length - 1; i >= 0; i--) {
            let last = cc.sys.localStorage.getItem(common.init.bookdata[i].id + '_last');
            if (last) {
                common.init.bookdata[i].last = last;
            }

        }
        common.init.bookdata = common.init.bookdata.sort(function(a, b) {
            //cc.log('a+'+a);
            return a.last < b.last;
        });
        self.node.removeAllChildren(true);
        //var l=null;
        for (var i = 0; i < common.init.bookdata.length; i++) {
            let bookface = cc.instantiate(self.book_prefab);
            //bookface.getComponent(cc.Sprite).spriteFrame.setTexture
            //booklocal[i]
            //cc.log(bookface.getComponent('bookface'));
            let bookfacescript = bookface.getComponent('bookface');
            //cc.log(bookfacescript);
            //换成ID
            let readcount = cc.sys.localStorage.getItem(common.init.bookdata[i].id + '_readcount') || '0';
            bookface.bookid = common.init.bookdata[i].id;
            bookface.mindex = i;
            bookface.page = (readcount == '0' ? 1 : readcount);

            bookfacescript.setBookName(readcount + "/" + common.init.bookdata[i].pages.length + ' ' + common.init.bookdata[i].bname);
            if (common.init.bookdata[i].bg != '') {
                cc.log(common.init.bookdata[i].bg);
                bookfacescript.setBookFace(common.init.bookdata[i].bg);
            }
            bookface.parent = self.node;
            // l=bookface;
        }

    }

});